package com.lai.note.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lai.note.activity.ModifyNoteActivity;
import com.lai.note.R;
import com.lai.note.model.Note;
import com.lai.note.util.ListNoteUtil;

import java.util.List;


public class NoteRecyclerAdapter extends RecyclerView.Adapter<NoteRecyclerAdapter.ViewHolder> {
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView title;
        private final TextView content;
        private final TextView saveTime;
        private Button btn_edit;
        private Button btn_del;
        private LinearLayout layout;
        private ViewHolder(View view){
            super(view);
            title = view.findViewById(R.id.tv_title);
            content = view.findViewById(R.id.tv_content);
            saveTime = view.findViewById(R.id.tv_save_time);
            btn_del = view.findViewById(R.id.btn_del);
            layout = view.findViewById(R.id.layout_note);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("编辑监听接口");
                    Note note = NoteRecyclerAdapter.notes.get(getAdapterPosition());
                    Intent intent = new Intent(view.getContext(), ModifyNoteActivity.class);
                    intent.putExtra("noteId",note.getNoteId());
                    intent.putExtra("title",note.getTitle());
                    intent.putExtra("content",note.getContent());
                    System.out.println(intent.toString());
                    view.getContext().startActivity(intent);

                }
            });
            btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("删除监听接口");
                    ListNoteUtil.userDeleteNote(view.getContext(),NoteRecyclerAdapter.notes.get(getAdapterPosition()).getNoteId());
                    System.out.println("删除成功");
                }
            });
        }
        public TextView getTitle(){
            return title;
        }
        public TextView getContent(){
            return content;
        }
        public TextView getSaveTime(){
            return saveTime;
        }
        public LinearLayout getLayout(){
            return layout;
        }
    }
    public static  List<Note> notes;

    public NoteRecyclerAdapter(List<Note> notes){
        super();
        this.notes = notes;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listnote_noteitem,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Note note = notes.get(position);
        holder.getTitle().setText(note.getTitle());
        holder.getContent().setText(note.getContent());
        holder.getSaveTime().setText(String.valueOf(note.getCreateTime()));
        if(position%2==0){
            holder.getLayout().setBackgroundColor(0xFDF0F0F9);
        }
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    //刷新界面的数据，这些数据从后台获取，相对显示较慢
    public void refresh(List<Note> notesList){
        notes = notesList;
        notifyDataSetChanged();
    }



}
